package com.kc.gb.device.common.adapter;

import com.kc.gb.base.dto.Result;
import com.kc.gb.device.common.adapter.dto.basic.rack.SimpleRackDto;
import com.kc.gb.device.common.adapter.dto.basic.rack.SimpleRackSelection;
import com.kc.gb.device.common.adapter.dto.basic.slot.SlotDto;
import com.kc.gb.device.common.adapter.dto.basic.slot.SlotSelection;
import com.kc.gb.device.common.adapter.dto.wes.station.dto.StationSlotDTO;
import com.kc.gb.device.common.adapter.dto.wes.station.dto.StationSlotQuery;
import com.kc.gb.device.common.adapter.feigns.BasicStationFeignClient;
import com.kc.gb.device.common.adapter.feigns.WesStationFeign;
import com.kc.gb.device.common.adapter.dto.basic.station.dto.BasicStationDTO;
import com.kc.gb.device.common.adapter.dto.wes.station.dto.WesStationDTO;
import com.kc.gb.device.common.adapter.dto.wes.station.dto.StationQuery;
import com.kc.gb.device.common.contants.CacheConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class StationAdaptor {

    @Resource
    WesStationFeign wesStationFeign;

    @Resource
    BasicStationFeignClient phoenixBasicFeign;

    /**
     * 根据 deviceCode 获取工作站信息
     * @param warehouseId
     * @param deviceCode
     * @return
     */
    @Cacheable(key = "'device:common:adapter:wes:station:bydevice:'+#deviceCode",cacheNames = {CacheConst.Caffeine.MANAGER_3MIN})
    public WesStationDTO getWesStationByDeviceCode(long warehouseId, String deviceCode){
        StationQuery stationQuery = new StationQuery();
        stationQuery.setDeviceCodes(Arrays.asList(deviceCode));
        Result<List<WesStationDTO>> result =  wesStationFeign.selectStations(warehouseId, stationQuery);
        log.info("getWesStationByDeviceCode:{}",result);
        return CollectionUtils.emptyIfNull(result.getData()).stream().findFirst().orElse(null);
    }

    /**
     * 根据 stationCode 获取 工作站基础信息
     * @param warehouseId
     * @param stationCode
     * @return
     */
    @Cacheable(key = "'device:common:adapter:wes:station:'+#stationCode",cacheNames = {CacheConst.Caffeine.MANAGER_3MIN})
    public WesStationDTO getWesStationByStationCode(long warehouseId, String stationCode){
        StationQuery query = new StationQuery();
        query.setStationCodes(Arrays.asList(stationCode));
        Result<List<WesStationDTO>> result =  wesStationFeign.selectStations(warehouseId, query);
        log.info("getWesStationByStationCode:{}",result);
        return CollectionUtils.emptyIfNull(result.getData()).stream().findFirst().orElse(null);
    }

    /**
     * 获取工作站槽位信息
     * @param warehouseId
     * @param stationCode
     * @return
     */
    @Cacheable(key = "'device:common:adapter:wes:station:slots:'+#stationCode",cacheNames = {CacheConst.Caffeine.MANAGER_3MIN})
    public List<StationSlotDTO> getWesStationSlots(long warehouseId, String stationCode){
        StationSlotQuery query = new StationSlotQuery();
        query.setStationCodes(Arrays.asList(stationCode));
        Result<List<StationSlotDTO>> result =  wesStationFeign.selectStationSlots(warehouseId,query);
        log.info("getWesStationSlots:{}",result);
        return result.getData();
    }

    /**
     * 获取 RCS 基础工作站信息
     * @param warehouseId
     * @param stationCode
     * @return
     */
    public List<BasicStationDTO> getAllBasicStation(long warehouseId, String stationCode){
        StationQuery query = new StationQuery();
        query.setStationCodes(Arrays.asList(stationCode));
        Result<List<BasicStationDTO>> result =  phoenixBasicFeign.findStationSlots(warehouseId);
        log.info("getAllBasicStation:{}",result);
        return result.getData();
    }

    @Cacheable(key = "'device:common:adapter:basic:station:'+#stationCode",cacheNames = {CacheConst.Caffeine.MANAGER_3MIN})
    public BasicStationDTO getBasicStation(long warehouseId, String stationCode){
        StationQuery query = new StationQuery();
        query.setStationCodes(Arrays.asList(stationCode));
        Result<BasicStationDTO> result =  phoenixBasicFeign.getByCode(warehouseId,stationCode);
        log.info("getBasicStation:{}",result);
        return result.getData();
    }

    public List<SimpleRackDto> getBasicRacks(long warehouseId, List<String> rackCodes){
        SimpleRackSelection query = new SimpleRackSelection();
        query.setCodes(rackCodes);
        Result<List<SimpleRackDto>> result =  phoenixBasicFeign.querySimpleRack(warehouseId,query);
        log.info("getBasicRacks:{}",result);
        return result.getData();
    }

    public List<SlotDto> getBasicSlotsByRackCode(long warehouseId, List<String> rackCodes){
        SlotSelection query = new SlotSelection();
        query.setRackCode(rackCodes);
        Result<List<SlotDto>>result =  phoenixBasicFeign.findStationSlots(warehouseId,query);
        log.info("getBasicSlotsByRackCode:{}",result);
        return result.getData();
    }

    public List<SlotDto> getBasicSlotsByRackId(long warehouseId, List<Long> rackIds){
        SlotSelection query = new SlotSelection();
        query.setRackId(rackIds);
        Result<List<SlotDto>>result =  phoenixBasicFeign.findStationSlots(warehouseId,query);
        log.info("getBasicSlotsByRackId:{}",result);
        return result.getData();
    }
}
